<?php 
function GetCategories()
{
        $conn = getConnection();
        $result = $conn->query('SELECT * FROM Categories P');
        return $result;
}
function GetUsers()
{
        $conn = getConnection();
        $result = $conn->query('SELECT * FROM People P');
        return $result;
}
function GetConnectMeths($person_id = Null)
{
        $conn = getConnection();
        $sql =  'SELECT CM.*, C.Name as Category_Name, CONCAT(P.First_Name, " ", P.Last_Name) as Person_Name '
                .       'FROM ConnectionMethods CM '
                .       '       JOIN People P ON CM.Person_ID = P.ID '
                .       '       JOIN Categories C ON CM.Category_ID = C.ID ';
        if($person_id != Null)
                $sql .= " WHERE Person_ID=$person_id ";
        $result = $conn->query($sql);
        return $result;
}
function GetConnectMeth($id)
{
        $conn = getConnection();
        $sql =  'SELECT CM.*, C.Name as Category_Name, CONCAT(P.First_Name, " ", P.Last_Name) as Person_Name '
                .       'FROM ConnectionMethods CM '
                .       '       JOIN People P ON CM.Person_ID = P.ID '
                .       '       JOIN Categories C ON CM.Category_ID = C.ID '
                .       "WHERE CM.ID=$id";
        //echo $sql;
        $result = $conn->query($sql);
        $rs = $result->fetch_assoc();
        $conn->close();
        return $rs;
}


function SaveConnectMeth($rs)
{ 
		$conn = getConnection();
		$rs = RsEscape($conn, $rs);
        $result = $conn->query("UPDATE ConnectionMethods Con SET Person_ID='$rs[Person_ID]', Category_ID='$rs[Category_ID]', Value='$rs[Value]', WHERE ID=$rs[ID]");     
        $error = $conn->error;
        $conn->close();
        return $error ? array('Server Error'=> $error) : Null;
        
}

function ValidateConnectMeth($rs)
{ 
	    $errors = Null;
        return $errors;
}

function DeleteConnectMeth($id)
{
        $conn = getConnection();
        $result = $conn->query("DELETE FROM ConnectionMethods WHERE ID=$id");
        $error = $conn->error;
        $conn->close();
        
        return $error ? array('Server Error' => $error) : Null;

}

function AddConnectMeth(&$rs)
{
		$conn = getConnection();
        $rs = EscapeRS($conn, $rs);
        $sql = "INSERT INTO ConnectionMethods 
        		.(Person_ID, Category_ID, Value, Created_at) "
                ."VALUES ($rs[Person_ID], $rs[Category_ID], '$rs[Value]', Now())";
        $result = $conn->query($sql);       
        $error = $conn->error;
        if(!$error)$rs['ID'] = $conn->insert_id;
        $conn->close();
        return $error ? array('Server Error' => $error) : Null;
}

function NewConnectMeth()
{
	return Null;
}
?>